﻿using Aierp.SystemEntities.System;
using Aierp.Utility.Data;

namespace Aierp.SystemRepository.Implements
{
    /// <summary>
    /// 按钮
    /// </summary>
    public class ButtonsRepository : DbInstance
    {
        /// <summary>
        /// 查询一个名称--根据名称和按钮标记
        /// </summary>
        /// <param name="title"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public Sys_Buttons Find(string title, string code)
        {
            var result =  this.FindBy<Sys_Buttons>(c => c.ButtonText == title);
            if (result != null) return result;

            result = this.FindBy<Sys_Buttons>(c => c.ButtonTag == code);
            return result;
        }

        /// <summary>
        /// 同时查询名称和按钮标记
        /// </summary>
        /// <param name="title"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public Sys_Buttons FindByTitleAndCode(string title, string code)
        {
            var result = this.FindBy<Sys_Buttons>(c => c.ButtonText == title && c.ButtonTag == code);
            return result;
        }

        ///// <summary>
        ///// 查询一个编码-根据ID，只返回指定信息，密码等关键信息不返回
        ///// </summary>
        ///// <param name="key"></param>
        ///// <returns></returns>
        public Sys_Buttons FindById(int key)
        {
            return this.FindBy<Sys_Buttons>(c => c.KeyId == key);
        }

        /// <summary>
        /// 获取一个列表，供给Grid使用
        /// </summary>
        /// <param name="sortOrder"></param>
        /// <param name="limit"></param>
        /// <param name="offset"></param>
        /// <param name="searchTxt"></param>
        /// <returns></returns>
        public string GetList(string sortOrder, int limit, int offset, string searchTxt)
        {
            if (!string.IsNullOrEmpty(searchTxt))
            {
                return this.GetPageList<Sys_Buttons, object>(limit, offset, c => c.ButtonTag == searchTxt, c => c.Sortnum, sortOrder);
            }
            return this.GetPageList<Sys_Buttons, object>(limit, offset, null, c => c.Sortnum, sortOrder);

        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public int Add(Sys_Buttons item)
        {
            //先检查是否存在
            var model = this.FindById(item.KeyId);
            if (model != null) return -9;

            var ret = Db.Insert(item);

            return ret?.KeyId ?? 0;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public int Update(Sys_Buttons item)
        {
            //先检查是否存在
            var model = this.FindById(item.KeyId);
            if (model == null) return -2;

            //更新
            Db.TrackEntity(model);//在上下文中跟踪实体
            model.ButtonText = item.ButtonText;
            model.ButtonTag = item.ButtonTag;
            model.Sortnum = item.Sortnum;
            model.Remark = item.Remark;
            model.ButtonHtml = item.ButtonHtml;
            model.IconUrl = item.IconUrl;
            model.iconCls = item.iconCls;

            return Db.Update(model);//这时只会更新被修改的字段
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public int Remove(int key)
        {
            //先检查是否存在
            var model = this.FindById(key);
            if (model == null) return -2;

            return this.Delete<Sys_Buttons>(key);
        }
    }
}
